package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.my.vote.R;
import com.my.vote.entity.req.ReqLogin;
import com.my.vote.pst.user.IPstLogin;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述:登录
 *
 * @author
 * @date 2018/4/11
 */
public class LoginActivity extends AbstractActivity implements IPstLogin.IVLogin {


    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPwd)
    EditText etPwd;

    IPstLogin pstLogin;
    @BindView(R.id.cbSave)
    CheckBox cbSave;
    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.btLogin)
    Button btLogin;

    public static void startUI(Context context, int toast) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("Toast", toast);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        if (getIntent().getIntExtra("Toast", -1) == 1) {
            // 提示一下需要登录
            showToast("Replace with your login", 1);
        }
        tvLeft.setText("Login");
        tvRight.setText("Register");
    }


    @Override
    public IPresenter getPresenter() {
        if (pstLogin == null) {
            pstLogin = LoaderService.getService(IPstLogin.class);
        }
        return pstLogin;
    }

    @Override
    public void onSuccess() {
        finish();
    }

    @Override
    public void onError(String error) {
        // 登录失败提示
        showToast(error, 2);
    }

    @OnClick({R.id.tvLeft, R.id.tvRight, R.id.btLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLeft:
                finish();
                break;
            case R.id.tvRight:
                RegisterActivity.startUI(LoginActivity.this);
                finish();
                break;
            case R.id.btLogin:
                if (pstLogin == null) {
                    return;
                }
                ReqLogin login = new ReqLogin();
                login.setNumber(etName.getText().toString());
                login.setPwd(etPwd.getText().toString());
                login.setSavePwd(cbSave.isChecked());
                pstLogin.login(login);
                break;
        }
    }
}
